package com.foreveross.ccjd;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.foreveross.ccjd.utility.Utils;

public class RegisterActivity extends BaseActivity {

	private static final int MSG_CODE = 1;
	private static final int DELAYMILLIS = 1000;
	private int mLastMillis=60;
	private Handler mHandler = new Handler()
	{
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_CODE:
				mLastMillis--;
				if(mLastMillis>=0)
				{
					mBtnCode.setText("点此获取("+mLastMillis+")");
					sendMessageDelayed(msg, DELAYMILLIS);
				}else
				{
					mBtnCode.setEnabled(true);
					mBtnCode.setText("点此获取");
				}
				break;

			default:
				break;
			}
		};
	};
	private TextView mBtnCode;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		findViewById(R.id.btn_login).setOnClickListener(this);
		findViewById(R.id.btn_forget_pwd).setOnClickListener(this);
		mBtnCode = (TextView) findViewById(R.id.btn_code);
		mBtnCode.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			doLogin();
			break;
		case R.id.btn_forget_pwd:

			break;
		case R.id.btn_code:
			
			break;
		default:
			super.onClick(v);
			break;
		}
	}
	private void getCode()
	{
		mBtnCode.setEnabled(false);
		mHandler.sendEmptyMessage(MSG_CODE);
	}
	private void doLogin() {
		TextView usernameTv = (TextView) findViewById(R.id.et_username);
		TextView passwordTv = (TextView) findViewById(R.id.et_password);
		TextView codeTv = (TextView) findViewById(R.id.et_code);
		String username = usernameTv.getText().toString();
		if (TextUtils.isEmpty(username)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			usernameTv.startAnimation(shake);
			Toast.makeText(this, usernameTv.getHint(), Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (!Utils.isPhoneNum(username)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			usernameTv.startAnimation(shake);
			Toast.makeText(this,R.string.tip_account_error, Toast.LENGTH_SHORT)
			.show();
			return;
		}
		String password = passwordTv.getText().toString();
		if (TextUtils.isEmpty(password)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			passwordTv.startAnimation(shake);
			Toast.makeText(this, passwordTv.getHint(), Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String code = codeTv.getText().toString();
		if (TextUtils.isEmpty(code)) {
			Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
			codeTv.startAnimation(shake);
			Toast.makeText(this, codeTv.getHint(), Toast.LENGTH_SHORT)
					.show();
			return;
		}

	}

	@Override
	protected int getContentView() {
		return R.layout.register;
	}
}
